package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.ProductBrand;
import cn.wolfcode.trip.base.query.ProductBrandQuery;
import cn.wolfcode.trip.base.service.IProductBrandService;
import cn.wolfcode.trip.base.service.IProductCatalogService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商城目录品牌资源后台控制器
 */
@Controller
@RequestMapping("productBrand")
public class ProductBrandController {

    @Autowired
    private IProductBrandService productBrandService;

    @Autowired
    private IProductCatalogService productCatalogService;

    /**
     * 商城目录页面
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductBrandQuery qo, Model model){
        //查询所有目录
        model.addAttribute("catalogs",productCatalogService.listAll());
        model.addAttribute("pageInfo",productBrandService.query(qo));
        return "productBrand/list";
    }

    /**
     * 新增/修改
     * @param productBrand
     * @param file
     * @return
     */
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ProductBrand productBrand, MultipartFile file){
        if(file!=null&&file.getSize()>0){
            String url = UploadUtil.uploadQiniu(file);
            productBrand.setCoverUrl(url);
        }
        productBrandService.saveOrUpdate(productBrand);
        return new JsonResult();
    }

    /**
     * 根据商品目录id查询商品品牌
     */
    @RequestMapping("selectByCatalogId")
    @ResponseBody
    public List<ProductBrand> selectByCatalogId(Long catalogId){
        return  productBrandService.selectByCatalogIdId(catalogId);
    }
}
